#!/usr/bin/env python 
# encoding: utf-8 
# @Author: Jacqueline
# @file: 03三大切换之窗口切换.py
# @time: 2021/2/22 11:35
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome()
driver.implicitly_wait(3)
#
# driver.get('http://10.10.8.222:9999/nanchang-afhy/index.html#/eventManagement')
#
# user = driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/ul/li[2]/p/input[2]')
# pwd = driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/ul/li[3]/p/input[3]')
# login = driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/ul/li[4]/div/div/span')
#
# user.send_keys('15010755275')
# pwd.send_keys('12345678a')
#
# login.click()
driver.get('https://www.ketangpai.com/#/login')
driver.maximize_window()
'''不可点击
iframe标签切换
1.通过iframe标签的name属性 # driver.switch_to.frame('login_frame')
2.定位iframe标签再进行切换

'''
# driver.switch_to.frame('login_frame')
time.sleep(1)
account = driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/div[2]/div/div/form/div[1]/div/div/input').send_keys('ktp0216595585')
pwd = driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/div[2]/div/div/form/div[2]/div/div/input').send_keys('hjq123')
auto = driver.find_element_by_css_selector('<img data-v-949580a6="" src="/images/login/newbg.png">').click()
login = driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/div[2]/div/div/div[1]/div[1]/button').click()

loc_ifr = (By.ID,'login_frame')
iframe_ele = driver.find_element(*loc_ifr)
driver.switch_to.frame(iframe_ele)

loc_news = (By.XPATH,"//a[text()='新闻']")
loc_bj = (By.XPATH,"//ul[@id='main-list']//a[text()='北京']")
print('新闻的窗口句柄',driver.current_window_handle)
# 点击新闻
driver.find_element(*loc_news).click()
# 显式等待新窗口出现
WebDriverWait(driver,5,0.5).until(
    EC.new_window_is_opened(current_handles=driver.window_handles)
)
# 切换到新窗口
wins = driver.window_handles
driver.switch_to.window(wins[-1])


from selenium import webdriver
driver = webdriver.Chrome()

driver.find_element_by_id().click()
#————————————警告框alert js需要切换弹框————————————
alert = driver.switch_to.alert
# 点击确定
alert.accept()
# 点击取消
alert.dismiss()
# 获取弹框的提示文本
content = alert.text
print("弹出的提示内容",content)
driver.quit()
